//
//  BVCategoryViewController.m
//  BuyVegetable
//
//  Created by yushanchang on 16/9/28.
//  Copyright © 2016年 com.yinding. All rights reserved.
//

#import "BVCategoryViewController.h"
#import "BVHttpRequest.h"
#import "BVIndexTool.h"
#import "BVCategoryOneCell.h"
#import "BVCategoryTwoCell.h"
//#import "BVCategoryReusableView.h"
#import "BVGoodsViewController.h"
#import "BVCategoryTool.h"
#import "BVParentList.h"
#import "MBProgressHUD.h"

#define CATEGORYTWO_CELL_WIDTH      (ScreenW-80-10)
#define CATEGORYONE_CELL_IDENTIFIER @"BVCategoryOneCell"
#define CATEGORYTWO_CELL_IDENTIFIER @"BVCategoryTwoCell"
//#define HEADER_REUSABLE_VIEW        @"BVCategoryReusableView"

@interface BVCategoryViewController ()
@property (weak, nonatomic) IBOutlet UITableView *tableView;
@property (weak, nonatomic) IBOutlet UICollectionView *collectionView;
@property (nonatomic, strong) NSArray *parentArr;
@property (nonatomic, strong) NSArray *childArr;
@property (nonatomic, strong) BVCategoryTool *categoryTool;
@property (nonatomic, copy) NSString *parentId;

@end

@implementation BVCategoryViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self fetchParentList];
}

- (void)fetchParentList {
//    [self.hud show:YES];
    [self.categoryTool fetchParentListSuccess:^(NSArray *parentList) {
//        [self.hud hide:YES];
        self.parentArr = parentList;
        [self.tableView reloadData];
        
        /*默认选中第一行*/
        NSIndexPath *firstRow = [NSIndexPath indexPathForRow:0 inSection:0];
        [self.tableView selectRowAtIndexPath:firstRow animated:YES scrollPosition:UITableViewScrollPositionBottom];
        
        NSIndexPath *path = [NSIndexPath indexPathForItem:0 inSection:0];
        [self tableView:self.tableView didSelectRowAtIndexPath:path];
    } failure:^(NSError *error) {
        [self.hud hide:YES];
        [self showError:@"加载失败"];
    }];
}

- (void)fetchChildList {
    [self.hud show:YES];
    [self.categoryTool fetchChildListByParentID:self.parentId success:^(NSArray *childList) {
        [self.hud hide:YES];
        self.childArr = (NSMutableArray *)childList;
        [self.collectionView reloadData];
    } failure:^(NSError *error) {
        [self.hud hide:YES];
        [self showError:@"加载失败"];
    }];
}

- (void)setTableView:(UITableView *)tableView {
    _tableView = tableView;
    [tableView registerNib:NIB_NAMED(CATEGORYONE_CELL_IDENTIFIER) forCellReuseIdentifier:CATEGORYONE_CELL_IDENTIFIER ];
}

- (void)setCollectionView:(UICollectionView *)collectionView {
    _collectionView = collectionView;
    [collectionView registerNib:NIB_NAMED(CATEGORYTWO_CELL_IDENTIFIER) forCellWithReuseIdentifier:CATEGORYTWO_CELL_IDENTIFIER];
}

#pragma mark -
#pragma mark - UITableViewDelegate,UITableViewDataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.parentArr.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 50;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    BVCategoryOneCell *categoryone_cell = [tableView dequeueReusableCellWithIdentifier:CATEGORYONE_CELL_IDENTIFIER];
    categoryone_cell.categoryOneLabel.text = [self.parentArr[indexPath.row] name];
    if (indexPath.row == 0) {
        [categoryone_cell show_select];
    }
    return categoryone_cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [self setButtonIsSelectedWithIndexPath:indexPath];
    BVParentList *parentList = self.parentArr[indexPath.row];
    self.parentId = parentList.ID;
    [self fetchChildList];
}

- (void)setButtonIsSelectedWithIndexPath:(NSIndexPath *)indexPath{
    BVCategoryOneCell *categoryone_cell = [self.tableView cellForRowAtIndexPath:indexPath];
    
    if (!categoryone_cell.is_selected){
        for (int i = 0; i < 10; i++) {
            
            NSIndexPath *p = [NSIndexPath indexPathForRow:i inSection:0];
            BVCategoryOneCell *cell = [self.tableView cellForRowAtIndexPath:p];
            
            if (![categoryone_cell isEqual:cell]) {// 未选中的隐藏
                [cell hide_select];
            }else{
                [cell show_select];
            }
        }
    }
}

#pragma mark -
#pragma mark - UICollectionViewDataSource,UICollectionViewDelegate
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return self.childArr.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    BVCategoryTwoCell *categorytwo_cell = [collectionView dequeueReusableCellWithReuseIdentifier:CATEGORYTWO_CELL_IDENTIFIER forIndexPath:indexPath];
    BVChildList *childList = self.childArr[indexPath.row];
    categorytwo_cell.name.text = childList.name;
    return categorytwo_cell;
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
    return CGSizeMake(CATEGORYTWO_CELL_WIDTH/3, 115);
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    
    BVChildList *childList = self.childArr[indexPath.row];
    BVGoodsViewController *goods = [[BVGoodsViewController alloc] init];
    goods.name = childList.name;
    goods.childId = childList.ID;
    [self.navigationController pushViewController:goods animated:YES];
}

- (BVCategoryTool *)categoryTool {
    if (!_categoryTool) {
        _categoryTool = [[BVCategoryTool alloc] init];
    }
    return _categoryTool;
}

- (NSArray *)parentArr {
    if (_parentArr == nil) {
        _parentArr = [[NSArray alloc] init];
    }
    return _parentArr;
}

- (NSArray *)childArr {
    if (_childArr == nil) {
        _childArr = [[NSArray alloc] init];
    }
    return _childArr;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}

@end
